﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Runtime.InteropServices;
using DiasEngineDate;

namespace DiasEngineDate
{
    /// <summary>
    /// DateConfig.xaml 的交互逻辑
    /// </summary>
    public partial class DateConfig : Window
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        public DateConfig()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //读取配置文件
            this.Serveraddr.Text = Encryption_Decryption.Decrypt(IniReadValue("Server", "server", AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini"));
            this.sqlname.Text = Encryption_Decryption.Decrypt(IniReadValue("Database", "name", AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini"));
            this.sqlpwd.Password = Encryption_Decryption.Decrypt(IniReadValue("Database", "pwd", AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini"));
            this.dataname.Text = Encryption_Decryption.Decrypt(IniReadValue("Dataname", "data", AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini"));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //测试新建一个ini文件
            WritePrivateProfileString("Server", "server", Encryption_Decryption.Encrypt(Serveraddr.Text.Trim()), AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini");//经过加密保存到配置文件Config.ini
            WritePrivateProfileString("Database", "name", Encryption_Decryption.Encrypt(sqlname.Text.Trim()), AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini");
            WritePrivateProfileString("Database", "pwd", Encryption_Decryption.Encrypt(sqlpwd.Password), AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini");
            WritePrivateProfileString("Dataname", "data", Encryption_Decryption.Encrypt(dataname.Text.Trim()), AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini");
            TheDateConfig.OpacityMask = this.Resources["ClosedBrush"] as LinearGradientBrush;
            Storyboard std = this.Resources["ClosedStoryboard"] as Storyboard;
            std.Completed += delegate { this.Close(); };
            std.Begin();
        }

        //读取INI文件的方法
        static public string IniReadValue(string Section, string Key, string filepath)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, filepath);
            return temp.ToString();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            TheDateConfig.OpacityMask = this.Resources["ClosedBrush"] as LinearGradientBrush;
            Storyboard std = this.Resources["ClosedStoryboard"] as Storyboard;
            std.Completed += delegate { this.Close(); };
            std.Begin();
        }
    }
}
